package com.xgk.boot.module.core.service.truck;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgk.boot.framework.common.constant.ElmConstant;
import com.xgk.boot.framework.common.context.ElmOrderThreadContext;
import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xgk.boot.framework.security.core.util.SecurityFrameworkUtils;
import com.xgk.boot.module.core.controller.admin.truck.vo.TruckOrderOtherReqVO;
import com.xgk.boot.module.core.dal.entity.truck.TruckOrderCancelLogDO;
import com.xgk.boot.module.core.dal.entity.truck.TruckOrderFeeDetailDO;
import com.xgk.boot.module.core.dal.entity.truck.TruckOrderLogDO;
import com.xgk.boot.module.core.dal.entity.truck.TruckOrdersDO;
import com.xgk.boot.module.core.dal.iservice.truck.TruckOrderLogService;
import com.xgk.boot.module.core.dal.mapper.truck.TruckOrderLogMapper;
import com.xgk.boot.module.core.enums.biz.ChangeTypeEnum;
import org.springframework.stereotype.Service;

/**
 * 订单操作日志 服务实现
 */
@Service
public class TruckOrderLogServiceImpl extends ServiceImpl<TruckOrderLogMapper, TruckOrderLogDO> implements TruckOrderLogService {


    public PageResult<TruckOrderLogDO> pageOrdersLogs(TruckOrderOtherReqVO orderOtherReqVO){
        if(orderOtherReqVO == null || StrUtil.isBlank(orderOtherReqVO.getOrderCode())){
            return new PageResult<>();
        }
        LambdaQueryWrapperX<TruckOrderLogDO> boxWrapperX = new LambdaQueryWrapperX<>();
        boxWrapperX.eq(TruckOrderLogDO::getOrderCode, orderOtherReqVO.getOrderCode());
        boxWrapperX.orderByDesc(TruckOrderLogDO::getCreateTime);
//        boxWrapperX.orderByDesc(TruckOrderLogDO::getOlTimestamp);
        Page<TruckOrderLogDO> page=new Page<>(orderOtherReqVO.getCurrent(),orderOtherReqVO.getPageSize());
        Page<TruckOrderLogDO> pageRes = this.page(page, boxWrapperX);

        return new PageResult<>(pageRes.getRecords(), pageRes.getTotal(), orderOtherReqVO.getCurrent(), orderOtherReqVO.getPageSize());
    }

    public Boolean addTruckOrderLog( TruckOrdersDO order, String note) {
        TruckOrderLogDO orderLog = new TruckOrderLogDO();
        orderLog.setOpType("change");
        orderLog.setOrderStatusFrom(order.getOrderStatus());
        orderLog.setOrderStatusTo(order.getOrderStatus());
        orderLog.setOrderCode(order.getOrderCode());
        orderLog.setOlType(ChangeTypeEnum.FEE.getValue());
        Object ip = ElmOrderThreadContext.get(ElmConstant.REQUEST_IP);
        if(ip != null) {
            orderLog.setOlIp(ip.toString());
        } else {
            orderLog.setOlIp("system");
        }
        orderLog.setOrderId(order.getOrderId());
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        if(loginUserId != null) {
            orderLog.setUserId(loginUserId);
            orderLog.setUserName(SecurityFrameworkUtils.getLoginUserName());
        }else {
            orderLog.setUserId(0L);
            orderLog.setUserName("system");
        }
        orderLog.setOlComments(note);
        return this.save(orderLog);
    }

    public Boolean addTruckOrderCacncelLog(TruckOrdersDO order, TruckOrderCancelLogDO truckOrderCancelLogDO){
        TruckOrderLogDO orderLog = new TruckOrderLogDO();
        orderLog.setOpType("cancel");
        orderLog.setOrderStatusFrom(truckOrderCancelLogDO.getOrderStatusFrom());
        orderLog.setOrderStatusTo(truckOrderCancelLogDO.getOrderStatusTo());
        orderLog.setOrderCode(order.getOrderCode());
        orderLog.setOlType(ChangeTypeEnum.STATUS.getValue());
        Object ip = ElmOrderThreadContext.get(ElmConstant.REQUEST_IP);
        if(ip != null) {
            orderLog.setOlIp(ip.toString());
        } else {
            orderLog.setOlIp("system");
        }
        orderLog.setOrderId(order.getOrderId());
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        if(loginUserId != null) {
            orderLog.setUserId(loginUserId);
            orderLog.setUserName(SecurityFrameworkUtils.getLoginUserName());
        }else {
            orderLog.setUserId(0L);
            orderLog.setUserName("system");
        }
        orderLog.setOlComments(truckOrderCancelLogDO.getOlComments());
        return this.save(orderLog);
    }

}
